<template>
	<view class="contain">
		<view class="head w-[100%]"><image class="w-[100%]" src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/buybg.jpg" mode="widthFix"></image></view>
		<view class="mt-9 flex justify-center"><text class="mid blod">身份证绑定</text></view>
		<view class="mt-5 flex justify-center flex-col m-auto w-[80%]">
			<view>
				<u-input placeholder="请输入查询身份证"
				 type="text"
				:customStyle="{'width':'550rpx'}" 
				:placeholderStyle="{'color':'#FFF'}" border="bottom" v-model="bindcode" @change="changeinput" fontSize="18" color="#000" maxlength="19"></u-input>
			</view>
			<view class="mt-3">
				<u-button type="primary" shape="circle" text="确认绑定" @click="fushubindcheck"></u-button>
			</view>
			<view class="mt-3">
				<u-button type="primary" shape="circle" text="跳过" :customStyle="dcustomstyle" @click="skip"></u-button>
			</view>
		</view>
	</view> <!-- contain -->
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
import { getSchool } from '@/common/common.js'

const store = userStore();
let { hasLogin,userData } = userStore();

const dcustomstyle=reactive({
	"background":"#ccc",
	"border":"0px",
});

let schoolname=ref('');
let bindcode=ref('');

onLoad((evt)=>{
	if(evt!=null){
		console.log(evt);
		uni.setStorageSync("keyid",evt.flag);
		if(evt.nick!=null){
			schoolname.value=getSchool(evt.nick);
			uni.setStorageSync("school",schoolname.value);
		}
	}
});


onShow(()=>{
	if(userData.binduseridcard!=null && userData.binduseridcard!=''){
		bindcode.value=userData.binduseridcard;
	}
});




const changeinput=(e:any)=>{
	console.log("输入发生变化:",e);
	userData.binduseridcard=e;
}



const fushubindcheck=()=>{
	
	// if(userData.userinfo===null || hasLogin===false){
	// 	uni.$tools.Toast("会话失效请重新登录");
	// 	setTimeout(()=>{
	// 		uni.$tools.GoUrl('/pages/login/login',0);
	// 	},1000);
	// 	return;
	// }
	
	if(!/\S/.test(bindcode.value)){
		uni.$tools.Toast("请输入查询身份证");
		return;
	}
	
	// schoolname.value=uni.getStorageSync("school");
	// if(!/\S/.test(schoolname.value)){
	// 	uni.$tools.Toast("学校信息不存在");
	// 	return;
	// }
	
	if(!/\S/.test(store.phone)){
		uni.$tools.Toast("会话已失效,请重新登录");
		setTimeout(()=>{
			uni.$tools.GoUrl('/pages/login/login',0);
		},1000);
		return;
	}
	
	//store.phone='19194925355'; //测试
	let paraData={
		id:userData.userinfo.id,
		//phone:"19194925355",//测试的手机号码
		phone:store.phone, //登录的手机号码
		//直接用身份证，不需要学校
		//schoolname:schoolname.value, //'港城中学-附属医院',这里不能使用固定值，学校可能有多个
		bindcode: bindcode.value.replace(" ","").replace("（","(").replace("）",")"),
		sessionId:(uni.getStorageSync("sid")===null || uni.getStorageSync("sid")==="")?null:uni.getStorageSync("sid")
	}
	console.log("查询绑定参数:",paraData);
	uni.$tools.Reqeust('/api/fushubindcheck',paraData,'POST').then((res:any)=>{
			console.log('查询绑定结果:',res);
			if (res.data.code == 200) {
				//查询用户成功，是本校用户身份证里
				// let h5link="https://depression.fubianmed.com/h5/#/login?url=order-detail&organ=qingyun&batchId=54B5Yt3c6gN"; //附属医院专用检查链接
				// let accessUrl="/pages/webview/webview";
				// store.backRouter=h5link;
				// uni.$tools.GoUrl(accessUrl);
				
				//判断当前登录的手机号码，是否和上次绑定的手机号码一直
				let userBindPhone=res.data.userinfo.phone;
				
				//号码是之前绑定的号码
				if(userBindPhone===store.phone){
					//跳转到获取二维码保存页面
					uni.$tools.GoUrl("/pages/savefushuewm/savefushuewm");					
				}else{
					uni.$tools.Toast("当前绑定的身份证与上次登录的手机绑定的不一致!若需更绑，请联系管理员");
				}

			}else{
				uni.$tools.Toast(res.data.msg);
			}
	})
}


const skip=()=>{
	uni.$tools.GoUrl("/pages/mine/mine",2);
}
</script>

<style>
	page{
		background:#f6eade;
	}
</style>
<style lang='scss' scoped>
	@import "fushubindcheck.scss";
</style>